package com.bum.rss.site;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;

import com.bum.rss.exp.om.RssEntry;
import com.bum.rss.site.cat.CnnCat;
import com.bum.rss.site.util.BaseParser;

public class CnnSite extends Site implements CnnCat, InitializingBean {
	final HashMap<String, String>	CAT_CODES	= new HashMap<String, String>();
	final List<RssEntry>			CATEGORIES	= new ArrayList<RssEntry>(CATS.length);

	@Override
	public void afterPropertiesSet() throws Exception {
		for (int n = CATS.length, i = 0; i < n; i++) {
			CAT_CODES.put(CATS[i][0], CATS[i][2]);
			CATEGORIES.add(new RssEntry(getCode() + URL_SEPARATOR + CATS[i][0], CATS[i][1]));
		}
	}

	@Override
	public List<RssEntry> listSiteCats(String user) {
		return CATEGORIES;
	}

	@Override
	public List<RssEntry> listCatEntrys(String cat, String user) {
		String url = "http://edition.cnn.com/" + CAT_CODES.get(cat);
		String html = BaseParser.loadHtml(url, BaseParser.ANDROID_HEADERS, true);
		html = BaseParser.parseContent(html, "<li class=\"section_header\" >", "</ul> </div> </div>");

		List<RssEntry> entries = new LinkedList<RssEntry>();

		String[] parts = html.split("<li ");
		for (int i = 1; i < parts.length - 1; i++) {
			String part = parts[i];
			String link = BaseParser.parseContent(part, "href=\"", "\"");
			if (!link.startsWith("http://")) {
				link = "http://edition.cnn.com" + link;
			}
			link = getRssLink(link, cat);
			if (link == null) {
				continue;
			}

			String title = (i == 1) ? BaseParser.parseContent(part, "<span class=\"tline\">", "<") : BaseParser.parseContent(part, "\"> ", "<");
			entries.add(new RssEntry(link, title));
		}

		return entries;
	}

	@Override
	public String getRssLink(String httpLink) {
		return getRssLink(httpLink, "m");
	}

	// http://edition.cnn.com/2014/06/25/business/defterios-alitalia/index.html?c=business => rss://boyvn/m/nworld/1094
	protected String getRssLink(String httpLink, String catId) {
		if (StringUtils.isBlank(httpLink) || !httpLink.startsWith("http://edition.cnn.com/")) {
			return null;
		}

		String code = BaseParser.parseContent(httpLink, "http://edition.cnn.com/", "/index.html");
		if (StringUtils.isBlank(code)) {
			return null;
		}

		if (!code.substring(0, code.indexOf("/")).matches("[0-9]+")) {
			return null;
		}

		return getCode() + URL_SEPARATOR + catId + URL_SEPARATOR + code;
	}

	@Override
	public RssEntry loadEntry(String path, String user) {
		String url = "http://edition.cnn.com/" + path + "/?c=intl-homepage-t&page=0";

		String html = BaseParser.loadHtml(url, BaseParser.ANDROID_HEADERS, true);

		html = BaseParser.parseContent(html, "<li class=\"article\" id=\"article\"", "<div class=\"pagenav\">");
		String title = BaseParser.parseContent(html, "<span class=\"tline\">", "</span>");
		String time = BaseParser.parseContent(html, "<span class=\"artday\">", "</span>");
		String autho = BaseParser.parseContent(html, "<span class=\"byline\">", "</span>");
		html = BaseParser.parseContent(html, "<div class=\"articleContent\">", "</div> </div>");
		html = time + "<br/>" + autho + "<br/>" + html;
		RssEntry entry = new RssEntry();
		entry.setTitle(title);
		entry.setContent(html);
		return entry;
	}

	public RssEntry loadEntryWeb(String path, String user) {
		String url = "http://edition.cnn.com/" + path + "/";

		String html = BaseParser.loadHtml(url);
		html = BaseParser.parseContent(html, "<!--startclickprintinclude-->", "<script type=\"text/javascript\">");
		String title = BaseParser.parseContent(html, "<h1>", "</h1>");

		String thumb = BaseParser.parseContent(html, "thumb: '", "',");
		String content = BaseParser.parseContent(html, "<p class=\"cnnEditorialNote\">", null);
		if (StringUtils.isNotBlank(thumb)) {
			content = "<img src=\"" + thumb + "\"/><br/>" + content;
		}

		RssEntry entry = new RssEntry();
		entry.setTitle(title);
		entry.setContent(content);
		return entry;
	}

	public static void main(String[] args) {
		CnnSite site = new CnnSite();
//		RssEntry entry = site.loadEntry("2014/06/27/news/luis-suarez-sponsors", null);
//		System.out.println(entry.getTitle());
//		System.out.println(entry.getContent());

		List<RssEntry> entries = site.listCatEntrys("1", null);
		if (entries != null) {
			for (RssEntry e : entries) {
				System.out.println(e.getUrl() + " " + e.getTitle());
			}
		}
	}
}
